package com.ljf.tijian.service;

import com.ljf.tijian.dto.ApiResponse;
import com.ljf.tijian.dto.CheckmealDetailedDto;
import com.ljf.tijian.dto.ReportDetailedDto;
import com.ljf.tijian.mapper.CidetailedreportMapper;
import com.ljf.tijian.pojo.Cidetailedreport;
import com.ljf.tijian.service.Impl.CidetailedReportServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class CidetailedReportService implements CidetailedReportServiceImpl {

    @Autowired
    CidetailedreportMapper cidetailedreportMapper;

    @Override
    public ApiResponse getdetaileddata(Cidetailedreport cidetailedReport) {

        List<Cidetailedreport> cidetailedreports = cidetailedreportMapper.selectByOrderId(cidetailedReport.getOrderId());
        List<ReportDetailedDto> list = new ArrayList<>();
        List<Integer> listciId = new ArrayList<>();
        for (Cidetailedreport cidetailedreport : cidetailedreports) {
            if(listciId.contains(cidetailedreport.getCiId())) continue;
            listciId.add(cidetailedreport.getCiId());
        }
        for (Integer i : listciId) {
            ReportDetailedDto reportDetailedDto = new ReportDetailedDto();
            reportDetailedDto.setCiId(i);
            reportDetailedDto.setList(new ArrayList<>());
            list.add(reportDetailedDto);
        }
        for (Cidetailedreport cidetailedreport : cidetailedreports) {
            for (ReportDetailedDto reportDetailedDto : list) {
                if(reportDetailedDto.getCiId() == cidetailedreport.getCiId()){
                    reportDetailedDto.getList().add(cidetailedreport);
                }
            }
        }


        return new ApiResponse(200,"SUCCESS",list);
    }

    @Override
    public ApiResponse updatereportdata(CheckmealDetailedDto checkmealDetailedDto) {
        List<Cidetailedreport> list = checkmealDetailedDto.getList();
        for (Cidetailedreport cidetailedreport : list) {
            Double maxrange = cidetailedreport.getMaxrange();
            Double minrange = cidetailedreport.getMinrange();
            if(maxrange!=null && minrange!=null){
                Integer value = Integer.parseInt(cidetailedreport.getValue());
                if(value<minrange || value>maxrange){
                    System.out.println(cidetailedreport.getName()+"有异常!");
                    cidetailedreport.setIsError(1);
                }else{
                    cidetailedreport.setIsError(0);
                }
            }
            cidetailedreportMapper.updateByPrimaryKeySelective(cidetailedreport);
        }
        return new ApiResponse(200,"SUCCESS");
    }
}
